/***尚硅谷Java数据结构与java算法                  ***/
/***https://www.bilibili.com/video/BV1E4411H73v***/
/***使用数组实现一个栈                           ***/
/***C++ 实现版本    written by Daniel           ***/
#include <iostream>
using std::cout;
using std::endl;
template <class T>
class Stack
{
public:

    Stack(int maxSize)
    {
        this->maxsize=maxSize;
        arrayStack=new T[maxsize];
        return;
    }
    bool isEmpty()
    {
        return top == -1;
    }
    bool isFull()
    {
        return top == (maxsize - 1);
    }
    void push(T value)
    {
        if (isFull()){
            cout<<"栈满，添加元素失败！"<<endl;
            return;
        }
        arrayStack[++top]=value;
        return;
    }
    T pop()
    {
        if (isEmpty()){
            cout<<"栈空，无法取出元素！\n"<<endl;
            return -1;
        }
        return arrayStack[top--];
    }
    ~Stack()
    {
        if (arrayStack!=nullptr){
            delete [] arrayStack;
            arrayStack=nullptr;
        }
        cout<<"析构成功"<<endl;
    }
    void list()
    {
        int temp=top;
        if (this->isEmpty()){
            cout<<"栈为空！"<<endl;
            return;
        }
        for(int i=temp;i>-1;i--){
            cout<<"stack["<<i<<"]"<<"="<<arrayStack[i]<<endl;
            
        }
        return;
    }
private:
    int maxsize;
    T* arrayStack;
    int top = -1;
};

int main(int argc, char** argv)
{
    Stack<int> s1(10);
    cout<<"s1 is empty?"<<s1.isEmpty()<<endl;
    s1.push(10);
    s1.push(20);
    s1.push(30);
    cout<<"s1 is empty?"<<s1.isEmpty()<<endl;
    cout<<"s1 is full?"<<s1.isFull()<<endl;
    s1.list();
    cout<<s1.pop()<<endl;
    cout<<s1.pop()<<endl;
    cout<<s1.pop()<<endl;
    cout<<"s1 is empty?"<<s1.isEmpty()<<endl;
    system("pause");
    return 0;
}